<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>教室管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->

    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}">
    <link rel="stylesheet" th:href="@{/js/lay-module/dtree/dtree.css}">
    <link rel="stylesheet" th:href="@{/js/lay-module/dtree/font/dtreefont.css}">

</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <!-- 左树 -->
        <div class="layui-col-sm12 layui-col-md4 layui-col-lg2">
            <div class="layui-card" style="overflow: auto;">
                <ul id="ltTree" class="dtree" data-id="0"></ul>
            </div>
        </div>
        <!-- 右表 -->
        <div class="layui-col-sm12 layui-col-md8 layui-col-lg10">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <button id="btnAdd" class="layui-btn icon-btn"  >
                                    <i class="layui-icon">&#xe654;</i>新增
                                </button>
                                <button id="btnDel" class="layui-btn layui-btn-danger icon-btn"  >
                                    <i class="layui-icon">&#xe640;</i>删除
                                </button>
                            </div>
                        </div>
                    </div>
                    <table class="layui-table" id="rtTable" lay-filter="rtTable"></table>
                </div>
            </div>
        </div>

    </div>
</div>

<script type="text/html" id="tool-table-bar">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
</script>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery.form/4.3.0/jquery.form.min.js"></script>

<script type="text/javascript" th:src="@{/lib/layui-v2.6.3/layui.js}"></script>


<script th:inline="javascript" type="application/javascript">

    layui.extend({
        dtree: '{/}../js/lay-module/dtree/dtree'
    })
    layui.use(['element','layer', 'table', 'code' ,'util', 'dtree', 'form'], function () {
        var element = layui.element,
        layer = layui.layer,
        table = layui.table,
        util = layui.util,
        dtree = layui.dtree,
        form = layui.form,
        $ = layui.$;

        // 树形渲染
        dtree.render({
            elem: '#ltTree',
            url: '/classroom/leftMenu',//
            method: 'GET',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            dataFormat: "list",  //配置data的风格为list
            // initLevel: '1',//默认展开层级，当该值大于level时，则会展开树的节点，直到不大于当前待展开节点的level
            response:{message:"msg",statusCode:0},  //修改 response中返回数据的定义
            ficon: [1,-1],
            icon: "2",  //修改二级图标样式

        });

        // 树形点击事件
        dtree.on('node("ltTree")', function (obj) {
            var data = obj.param;
            layer.msg('你选择了：' + data.nodeId + '-' + data.context);
            //table.reload('rtTable', {where: {nodeId: data.nodeId}});
            ins1(data.nodeId,"",data.context);
        });

        ins1("","","");

        // 渲染表格
        function ins1(pid,option,Keyword){
            table.render({
                elem: '#rtTable',
                url: '/classroom/list',
                cols: [
                [
                    {type: 'checkbox'},
                    {field: 'id', align: 'center', sort: true, title: '教室Id'},
                    {field: 'campusCode', align: 'center', sort: true, title: '校区代码'},
                    {field: 'campusName', align: 'center', sort: true, title: '校区名称'},
                    {field: 'academicName', align: 'center', sort: true, title: '教学楼名称'},
                    {field: 'classroomCode', align: 'center', sort: true, title: '教室代码'},
                    {field: 'classroomName', align: 'center', sort: true, title: '教室名称'},
                    {field: 'typeName', align: 'center', sort: true, title: '教室类型名称'},
                    {align: 'center', align: 'center', toolbar: '#tool-table-bar', title: '操作', minWidth: 120}
                ]],
                page: true,
                limit: 15,   // 默认十条数据一页
                limits: [15, 20, 25, 30, 50],  //数据分页条
                cellMinWidth: 100,
                where: {keyword:Keyword,option:option || ''}   // 提交的参数
            });
        }

        // 监听列表行工具条
        table.on('tool(rtTable)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'edit') { // 编辑
                // showEditModel(data);
                layer.msg('点击了编辑');
                console.log(JSON.stringify(data))
                // 显示编辑表单
                var index = layer.open({
                    title: '编辑数据',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/classroom/edit?id=' + data.id,
                    end: function(){
                        window.location.reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (layEvent === 'del') { // 删除
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:'/classroom/delete',
                        type:'get',
                        data:{'id': obj.data.id},//向服务端发送删除的id
                        success:function(suc){
                            if(  suc.code == 0 ){
                                layer.close(index);
                                obj.del();
                                layer.close(index);
                                layer.msg(suc.msg,{icon:1});
                                window.location.reload();
                            }
                            else{
                                layer.msg(suc.msg,{icon:5});
                            }
                        }
                    });
                });
            }
        });

        // 增加
        $('#btnAdd').click(function () {
            layer.msg("添加按钮");
            var index = layer.open({
                title: '添加数据',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/classroom/add',
                end: function(){
                    window.location.reload();
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        });

        // 批量删除
        $('#btnDel').click(function () {
            var checkRows = table.checkStatus('rtTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要删除的数据', {icon: 2});
            } else {
                var deletedata = [];
                for(var i=0;i<checkRows.data.length;i++){
                    deletedata.push(checkRows.data[i].dictId);
                }
                deleteDictionary(deletedata);
            }
        });

    });

</script>

</body>
</html>